import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.events.Event;
import flash.filters.DropShadowFilter;

class Loading extends Sprite {
	
	var tf:TextField;
	
	public function new(text:String) {
		super();

		this.tf = new TextField();
		this.tf.text = text;
		
		var format:TextFormat = new TextFormat();
		format.color = 0x000000;
		format.font = "Verdana";
		format.size = 12;
		//format.align = "center";
		format.align = flash.text.TextFormatAlign.CENTER;
		
		this.tf.setTextFormat(format);
		//this.tf.autoSize = "left";
		this.tf.autoSize = flash.text.TextFieldAutoSize.LEFT;
		this.tf.x = 5;
		this.tf.y = 5;
		
		//
		// HACK! For some reason the Stage.height is not
		// correct the very first time this object is created
		// so we wait untill the first frame before placing
		// the movie clip at the center of the Stage.
		//
		
		this.addEventListener( Event.ENTER_FRAME, this.onEnter );
			
		this.addChild( this.tf );
		
		this.graphics.lineStyle( 2, 0x808080, 1 );
		this.graphics.beginFill( 0xf0f0f0 );
		this.graphics.drawRoundRect(0, 0, this.tf.width + 10, this.tf.height + 10, 5, 5);
		
		var spin:Sprite = new Sprite();
		spin.x = this.tf.width + 40;
		spin.y = (this.tf.height + 10) / 2;
		
		var radius:Int = 15;
		var dots:Int = 6;
		var colours:Array<Dynamic> = [0xF0F0F0,0xD0D0D0,0xB0B0B0,0x909090,0x707070,0x505050,0x303030];
		
		for( i in 0...dots)
		{
			var deg:Float = (360/dots)*i;
			var radians:Float = deg * (Math.PI/180);
			var x:Float = radius * Math.cos(radians);
			var y:Float = radius * Math.sin(radians);
			
			spin.graphics.lineStyle(0, 0, 0);
			spin.graphics.beginFill( colours[i], 1 );
			spin.graphics.drawCircle( x, y, 4 );
		}
		
		this.addChild( spin );

		var dropShadow:DropShadowFilter = new DropShadowFilter();
		dropShadow.blurX = 4;
		dropShadow.blurY = 4;
		dropShadow.distance = 4;
		dropShadow.angle = 45;
		dropShadow.quality = 2;
		dropShadow.alpha = 0.5;
		// apply shadow filter
		this.filters = [dropShadow];
	
	/*
	
		
		
		spin.onEnterFrame = function ()
		{
			this._rotation += 5;
		}
	
		*/
	}
	
	function onEnter(event:Event):Void {
		
		if( this.stage != null) {
			this.x = (this.stage.stageWidth/2)-((this.tf.width+10)/2);
			this.y = (this.stage.stageHeight/2)-((this.tf.height+10)/2);
			// this.removeEventListener( Event.ENTER_FRAME, this.onEnter );
			// trace('ppp');
		}
		this.getChildAt(1).rotation += 5;
	}

}

